<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | Template管理
// +----------------------------------------------------------------------
namespace app\templates\controller;
use think\Db;
use app\common\controller\Adminbase;

class Template extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
        
    }

    /**
     * template列表
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $files = scandir('../templates/default/');
            foreach($files as $key => $value){
                if($value == '.' || $value == '..'){
                    unset($files[$key]);continue;
                }
                $url = '<a data-full="true" data-open="' . url('info', ['path' => $value]) . '"">' . $value . '</a>';
                $files[$key] = ['id'=>$key,'path'=>$value,'url'=>$url];
            }
            $result = array("code" => 0, "count" => count($files), "data" => $files);
            return json($result);
        }
        return $this->fetch();
    }
    /**
     * template文件列表
     */
    public function info(){
        if ($this->request->isAjax()) {
            $param = $this->request->param();       
            $files = scandir('../templates/default/'.$param['path'].'/');
            foreach($files as $key => $value){
                if($value == '.' || $value == '..'){
                    unset($files[$key]);continue;
                }
                $language = $param['path'] == 'cms' ? 'default' : $param['path'];
                $cates = Db::name('category')->where('setting','like','%'.$value.'%')->where('language',$language)->group('catname')->column('catname');
                $catname = implode(',',$cates);
                if($value == 'index.html'){
                    $catname = '首页';
                }else if($value == 'footer.html'){
                    $catname = '底部';
                }else if($value == 'header.html'){
                    $catname = '头部';
                }
                $url = '<a data-full="true" data-open="' . url('edit', ['filename' => $value, 'path' => $param['path']]) . '"">' . $value . '</a>';
                $files[$key] = ['id'=>$key,'filename'=>$value,'catname'=>$catname, 'url'=>$url];
            }
            $result = array("code" => 0, "count" => count($files), "data" => $files);
            return json($result);
        }
        $param = $this->request->param();
        $this->assign('path',$param['path']);
        return $this->fetch();
    }

    /**
     * template编辑
     */
    public function edit()
    {   
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $res = file_put_contents('../templates/default/'.$post['path'].'/'.$post['filename'],$post['content']);
            $res ? $this->success('修改成功！') : $this->error('修改失败！'); 
        }
        $param = $this->request->param();
        $content = file_get_contents('../templates/default/'.$param['path'].'/'.$param['filename']);
        $this->assign('path',$param['path']);
        $this->assign('filename',$param['filename']);
        $this->assign('content',htmlspecialchars_decode($content));
        return $this->fetch();
    }
}